package BookFiles
{
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;

    import mx.core.EdgeMetrics;
    import mx.core.IFlexDisplayObject;
    import mx.core.UIComponent;
    import mx.skins.Border;
    import mx.skins.halo.HaloBorder;

    public class BookSheet extends UIComponent
    {
        private var _leftPage:IFlexDisplayObject;
        private var _rightPage:IFlexDisplayObject;
        private var border:Border;

        public function BookSheet()
        {
            super();
        }

        override protected function createChildren():void
        {
            super.createChildren();
            var borderClass:Class = getStyle("borderSkin");
            if (borderClass == null)
                borderClass = HaloBorder;
            border = new borderClass();


            border.styleName = this;
            addChildAt(border, 0);
        }

        public function set leftPage(value:IFlexDisplayObject):void
        {
            if (value == _leftPage)
                return;
            if (_leftPage != null)
                removeChild(DisplayObject(_leftPage));

            _leftPage = value;

            if (_leftPage != null)
                addChild(DisplayObject(_leftPage));

            invalidateDisplayList();


        }

        public function get leftPage():IFlexDisplayObject
        {
            return _leftPage;
        }

        public function set rightPage(value:IFlexDisplayObject):void
        {
            if (value == _rightPage)
                return;
            if (_rightPage != null)
                removeChild(DisplayObject(_rightPage));

            _rightPage = value;

            if (_rightPage != null)
                addChild(DisplayObject(_rightPage));

            invalidateDisplayList();

        }

        public function get rightPage():IFlexDisplayObject
        {
            return _rightPage;
        }

        public function clearContent():void
        {
            leftPage = null;
            rightPage = null;
        }

        public function hasLeftPage():Boolean
        {
            return (leftPage != null);
        }

        public function hasRightPage():Boolean
        {
            return (rightPage != null);
        }

        public function hasContent():Boolean
        {
            return (leftPage != null || rightPage != null);
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            var left:Number = 17;
            var right:Number = 17;
            var top:Number = 17;
            var bottom:Number = 17;
            var spine:Number = 17;

            var metrics:EdgeMetrics = border.borderMetrics;

            left = left + metrics.left;
            right = right + metrics.right;
            top = top + metrics.top
            bottom = bottom + metrics.bottom;

            if (leftPage != null && rightPage == null)
            {
                border.setActualSize(unscaledWidth / 2, unscaledHeight);
                border.move(0, 0);

            }

            if (leftPage == null && rightPage != null)
            {
                border.setActualSize(unscaledWidth / 2, unscaledHeight);
                border.move(unscaledWidth / 2, 0);
            }
            if (leftPage != null && rightPage != null)
            {
                border.setActualSize(unscaledWidth, unscaledHeight);
                border.move(0, 0);
            }

            if (leftPage != null)
            {
                _leftPage.move(left, top);
                _leftPage.setActualSize(unscaledWidth / 2 - left - spine, unscaledHeight - top - bottom);
                if (_leftPage is UIComponent && UIComponent(_leftPage).initialized == false)
                    UIComponent(_leftPage).initialized = true;
            }

            if (rightPage != null)
            {
                _rightPage.setActualSize(unscaledWidth / 2 - right - spine, unscaledHeight - top - bottom);
                _rightPage.move(unscaledWidth / 2 + spine, top);
                if (_rightPage is UIComponent && UIComponent(_rightPage).initialized == false)
                    UIComponent(_rightPage).initialized = true;
            }

        }

        /*        public function copyInto(bitmap:BitmapData, side:String):void
           {
           cacheAsBitmap = false;
           validateNow();
           var m:Matrix = new Matrix();
           var rec:Rectangle;

           if (side == "right")
           {
           if (bitmap.width != unscaledWidth)
           {
           m.translate(-unscaledWidth / 2, 0);
           bitmap.draw(this, m);
           }
           else
           {
           rec = new Rectangle(unscaledWidth / 2, 0, unscaledWidth / 2, unscaledHeight);
           bitmap.draw(this, m, null, null, rec);
           }

           }

           if (side == "left")
           {
           if (bitmap.width != unscaledWidth)
           {
           bitmap.draw(this);
           }
           else
           {
           rec = new Rectangle(0, 0, unscaledWidth / 2, unscaledHeight);
           bitmap.draw(this, m, null, null, rec);
           }
           }
           cacheAsBitmap = true;


         }*/

        public function copyInto(bitmap:BitmapData, side:String):void
        {
            cacheAsBitmap = false;
            validateNow();

            if (_leftPage is UIComponent)
                UIComponent(_leftPage).validateNow();
            if (_rightPage is UIComponent)
                UIComponent(_rightPage).validateNow();

            var m:Matrix = new Matrix();
            var rc:Rectangle;
            if (side == "left")
            {
                if (bitmap.width < unscaledWidth)
                {
                    //                   m.translate(unscaledWidth / 2, 0);
                    var x:int = 0;
                }
                bitmap.draw(this, m, null, null, new Rectangle(0, 0, unscaledWidth / 2, unscaledHeight));
            }
            else if (side == "right")
            {
                if (bitmap.width < unscaledWidth)
                {
                    m.translate(-unscaledWidth / 2, 0);
                    rc = new Rectangle(0, 0, unscaledWidth / 2, unscaledHeight)
                }
                else
                {
                    rc = new Rectangle(unscaledWidth / 2, 0, unscaledWidth / 2, unscaledHeight)
                }
                bitmap.draw(this, m, null, null, rc);

            }
            else
                bitmap.draw(this);

            cacheAsBitmap = true;
        }

    }
}